import {unByKey} from 'ol/Observable';
import {Select} from 'ol/interaction';
import {click, pointerMove} from 'ol/events/condition';
import Gstyle from './Gstyle';
import {message} from 'antd'

class Ginteraction {
  constructor(map) {
    this.map = map;
    this.sel = [];
  }

  //缩放和移动
  on_moveend(callback) {
    this.moveend = this.map.on('moveend', callback);
  }

  off_moveend() {
    unByKey(this.moveend);
  }

  //鼠标移动
  on_pointermove(callback) {
    this.pointermove = this.map.on('pointermove', callback);
  }

  off_pointermove() {
    unByKey(this.pointermove);
  }

  //鼠标单击
  on_click(callback) {
    this.click = this.map.on('click', callback);
  }

  off_click() {
    unByKey(this.click);
  }

  //鼠标双击
  on_dblclick(callback) {
    this.dblclick = this.map.on('dblclick', callback);
  }

  off_dblclick() {
    unByKey(this.dblclick);
  }

  //选择要素：鼠标hover
  on_selectLayers_hover(callback,layers) {
    if (!this.select_interaction_hover) {
      this.select_interaction_hover = new Select({
        condition: pointerMove,
        style: Gstyle.pointermove_style,
        filter: function(feature, layer){
          if(layers.indexOf(layer)!=-1) {
            return true;
          }
        }
      });
      this.select_interaction_hover.on('select', callback);
    }
    this.map.addInteraction(this.select_interaction_hover);
  }
  //选择要素：鼠标hover
  on_select_hover(callback) {
    if (!this.select_interaction_hover) {
      this.select_interaction_hover = new Select({
        condition: pointerMove,
        style: Gstyle.pointermove_style
      });
      this.select_interaction_hover.on('select', callback);
    }
    this.map.addInteraction(this.select_interaction_hover);
  }

  off_select_hover() {
    this.map.removeInteraction(this.select_interaction_hover);
  }

  //选择要素：鼠标click
  on_select_click(id, type, callback) {
    if (!this.sel[id]) {
      let style = (type === "0") ? Gstyle.selectedPnt : (type === "1") ? Gstyle.selectedLine : Gstyle.selected;
      this.sel[id] = new Select({
        condition: click,
        style: style
      });
      this.sel[id].on('select', callback);
    }
    this.map.addInteraction(this.sel[id]);
  }

  off_select_click(id) {
    if (this.sel[id]) {
      this.map.removeInteraction(this.sel[id]);
      this.sel[id] = null;
    }
  }

  fly_to(f, type) {
    if (!f || !f.getGeometry()) {
      message.warn('无位置信息，无法定位！');
      return
    }
    let style = (type === "点图层") ? Gstyle.selectedPnt : (type === "线图层" ) ? Gstyle.selectedLine : Gstyle.selectedArea;
    f.setStyle(style);
    let geom = f.getGeometry();
    this.map.getView().fit(geom, {
      padding: [100, 100, 100, 100],
    });
  }
}
export default Ginteraction;
